package com.atguigu.Interceptor;

import jakarta.enterprise.inject.spi.Interceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {
    /**
     * 执行handler之前执行
     * 可以设置编码格式 ，登录保护，权限处理
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 我们要调用的方法对象
     * @return true 放行，false 不放行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor.preHandle");
        return true;
    }

    /**
     * 当handler执行之后，触发的方法
     * 可以做敏感词汇检查
     * @param request 请求
     * @param response 响应
     * @param handler handler方法
     * @param modelAndView 视图
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor.postHandle");
    }

    /**
     * 整齐处理完后触发
     * @param request 请求
     * @param response 响应
     * @param handler handler方法
     * @param ex 异常
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor.afterCompletion");

    }
}
